﻿using MelodysDemo.Application.ClazzQuestionnaires;

namespace MelodysDemo.API.Controllers
{
    [ApiController]
    [Route("[controller]/[action]")]
    public class ClazzQuestionnairesController : Controller
    {
        readonly IRepository<SurveyClazzQuestionnaire, int> _clazzQuestionnaire;//班级问卷信息表
        readonly IClazzQuestionnaireService _clazzQuestionnaireService;
        readonly IRepository<SurveyClazz, int> _clazz;//班级
        readonly IRepository<SurveyQuestionnaire, int> _questtionnaies;//问卷
        readonly IRepository<SurveyQuestion, int> _questtion;//问题
        readonly IRepository<SurveyOption, int> _option;//选项
        public ClazzQuestionnairesController(IRepository<SurveyClazzQuestionnaire, int> clazzQuestionnaire,
                                             IClazzQuestionnaireService clazzQuestionnaireService,
                                             IRepository<SurveyClazz, int> clazz,
                                             IRepository<SurveyQuestionnaire, int> questtionnaies,
                                             IRepository<SurveyQuestion, int> questtion,
                                             IRepository<SurveyOption, int> option)
        {
            _clazzQuestionnaire = clazzQuestionnaire;
            _clazzQuestionnaireService = clazzQuestionnaireService;
            _clazz = clazz;
            _questtionnaies = questtionnaies;
            _questtion = questtion;
            _option = option;
        }
        //查询
        [HttpGet]
        public async Task<IActionResult> ClazzQuestionnaires([FromQuery] ClazzQuestionnaireInput input)
        {
            var list = await _clazzQuestionnaireService.GetResultDto(input);
            return Json(new VueResult<CQnaireDto> { code = 0, msg = "请求成功", count = list.TotalCount, data = list.Data });
        }

        //获取班级和问卷
        [HttpGet]
        public async Task<IActionResult> GetClazzAndQnaires()
        {
            //获取正常、未删除的班级 
            var clazzs = await _clazz.GetAllListAsync(c => c.Status == 1 && c.IsDelete == 0);
            //获取已启用、未删除的班级
            var qnaires = await _questtionnaies.GetAllListAsync(q => q.Status == 1 && q.IsDelete == 0);
            return Json(new { code = 0, clazzs = clazzs, qnaires = qnaires });
        }

        //添加教学评价
        [HttpPatch]
        public async Task<IActionResult> CQnAdd([FromBody] SurveyClazzQuestionnaire cqn)
        {
            string uname = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.Name).Value;
            cqn.Status = 0;//待发布
            cqn.Version = 0;//版本号
            cqn.Creator =uname;
            cqn.Modifier =uname;
            cqn.CreateTime = DateTime.Now;//创建时间
            cqn.ModifiedTime = DateTime.Now;//修改时间
            cqn.IsDelete = 0;//默认显示
            SurveyClazzQuestionnaire? s = await _clazzQuestionnaire.InsertAsync(cqn);
            if (s != null)
            {
                return Json(new UIResult(0, "添加成功"));
            }
            return Json(new UIResult(-1, "添加失败"));
        }

        //修改
        [HttpPatch]
        public async Task<IActionResult> CQnEdit([FromBody] SurveyClazzQuestionnaire cqn)
        {
            var obj = await _clazzQuestionnaire.FirstOrDefaultAsync(x => x.Id == cqn.Id);
            string uname = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.Name).Value;
            obj.ClazzId = cqn.ClazzId;//班级Id
            obj.QuestionnaireId = cqn.QuestionnaireId;//问卷Id
            obj.IsIpLimit = cqn.IsIpLimit;//IP限制
            obj.Version++;//版本号
            obj.Modifier =uname;
            obj.ModifiedTime = DateTime.Now;//修改时间
            SurveyClazzQuestionnaire? s = await _clazzQuestionnaire.UpdateAsync(obj);
            if (s != null)
            {
                return Json(new UIResult(0, "修改成功"));
            }
            return Json(new UIResult(-1, "修改失败"));
        }

        //删除
        [HttpGet]
        public async Task<IActionResult> CQnDel([FromQuery] int id)
        {
            SurveyClazzQuestionnaire? obj = await _clazzQuestionnaire.FirstOrDefaultAsync(x => x.Id == id);
            string uname = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.Name).Value;
            obj.IsDelete = 1;//修改为删除
            obj.Modifier =uname;
            obj.ModifiedTime = DateTime.Now;
            SurveyClazzQuestionnaire s = await _clazzQuestionnaire.UpdateAsync(obj);
            if (s != null)
            {
                return Json(new UIResult(0, "删除成功"));
            }
            return Json(new UIResult(-1, "删除失败"));
        }

        //获取问卷信息前
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> QnDetails([FromQuery] int ClazzQuestionnaireId)
        {
            //1.获取对应的班级评价Id
            SurveyClazzQuestionnaire cqn = await _clazzQuestionnaire
                                           .FirstOrDefaultAsync(x => x.Id == ClazzQuestionnaireId);
            if (cqn == null)
            {
                return Json(new UIResult(-1, "请求失败"));
            }
            //2.获取班级和问卷
            SurveyQuestionnaire qnaire = await _questtionnaies
                                         .FirstOrDefaultAsync(x => x.Id == cqn.QuestionnaireId);
            SurveyClazz clazz = await _clazz.FirstOrDefaultAsync(x => x.Id == cqn.ClazzId);
            return Json(new { code = 0, clazz = clazz, qnaire = qnaire });
        }

        //获取问卷信息
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> QDetails([FromQuery] int ClazzQuestionnaireId)
        {
            //1.获取对应的班级评价Id
            SurveyClazzQuestionnaire cqn = await _clazzQuestionnaire
                                           .FirstOrDefaultAsync(x => x.Id == ClazzQuestionnaireId);
            if (cqn == null)
                return Json(new UIResult(-1, "请求失败"));
            else if (cqn.Status != 1)
                return Json(new UIResult(-1, "问卷未发布"));
            //2.根据班级评价信息中的 QuestionnaireId 获取问卷信息
            SurveyQuestionnaire qn = await _questtionnaies
                                     .FirstOrDefaultAsync(x => x.Id == cqn.QuestionnaireId);
            if (qn.Status !=1)
                return Json(new UIResult(-1, "问卷未启用"));
            //3.创建 SurveyOption 对象的集合
            // List<SurveyOption> listO = new List<SurveyOption>();
            //4.根据问卷Id循环获取对应的问题
            List<SurveyQuestion> qs = await _questtion
                                      .GetAllListAsync(x => x.QuestionnaireId == qn.Id);
            //5.根据问题Id找到对应的选项
            foreach (SurveyQuestion q in qs)
            {
                List<SurveyOption> os = await _option
                                        .GetAllListAsync(x => x.QuestionId == q.Id);
                q.SurveyOptions = os;
                //  listO.AddRange(os);
            }
            return Json(new { code = 0, qnaire = qn, questions = qs });
        }

        //设为已发布
        [HttpPatch]
        public async Task<IActionResult> CQnStatusON([FromQuery] int id)
        {
            SurveyClazzQuestionnaire? obj = await _clazzQuestionnaire.FirstOrDefaultAsync(x => x.Id == id);
            string uname = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.Name).Value;
            obj.Status = 1;//修改为已发布
            obj.Modifier =uname;
            obj.ModifiedTime = DateTime.Now;
            obj.Version++;
            SurveyClazzQuestionnaire s = await _clazzQuestionnaire.UpdateAsync(obj);
            if (s != null)
            {
                return Json(new UIResult(0, "已发布"));
            }
            return Json(new UIResult(-1, "数据请求异常"));
        }

        //设为已停止
        [HttpPatch]
        public async Task<IActionResult> CQnStatusOff([FromQuery] int id)
        {
            var obj = await _clazzQuestionnaire.FirstOrDefaultAsync(x => x.Id == id);
            string uname = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.Name).Value;
            obj.Status = 2;//修改为已停止
            obj.Modifier =uname;
            obj.ModifiedTime= DateTime.Now;
            obj.Version++;
            SurveyClazzQuestionnaire s = await _clazzQuestionnaire.UpdateAsync(obj);
            if (s != null)
            {
                return Json(new UIResult(0, "已停止"));
            }
            return Json(new UIResult(-1, "数据请求异常"));
        }

    }
}
